<template>
  <div>
    <TopFilter :list="topFilterList" :labelWidth="150" :cols="1" :collapse="false" :isSubmitBtn="false" />
    <div style="text-align:center;" class="dialogBottmView">
      <el-button size="small" type="primary" @click="saveDialog">确定</el-button>
      <el-button size="small" type="primary" @click="closePage">取消</el-button>
    </div>
  </div>
</template>

<script>
import { maintainRepairAssignComplete } from '@/api/repair/repairAssign/repairAssign';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import dictCode from '@/utils/dictCode';
export default {
  name: 'completedModel',
  props: ['Mileage'],
  data() {
    return {
      fetchParams: {},
      topFilterList: this.createTopFilterList()
    };
  },
  mounted() {
    this.serviceConsultant();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '进厂行驶里程',
          fieldName: 'IN_MILEAGE',
          placeholder: '请输入进厂行驶里程',
          style: { width: '200px' },
          initialValue: this.Mileage[0].IN_MILEAGE
        },
        {
          type: 'INPUT',
          label: '出厂行驶里程',
          fieldName: 'outMileage',
          placeholder: '请输入出厂行驶里程',
          style: { width: '200px' },
          initialValue: this.Mileage[0].OUT_MILEAGE,
          rules: [{ required: true, message: '请输入出厂行驶里程', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '试车员',
          fieldName: 'technician',
          placeholder: '请选择试车员',
          filterable: true,
          style: { width: '200px' },
          itemList: []
        },
        {
          type: 'SELECT',
          label: '终检人',
          style: { width: '200px' },
          fieldName: 'technicianName',
          placeholder: '请选择终检人',
          filterable: true,
          itemList: []
        }
      ];
    },
    async saveDialog() {
      if (this.topFilterList.find(item => item.fieldName === 'outMileage').initialValue) {
        let data = this.topFilterList.find(x => x.fieldName === 'IN_MILEAGE').initialValue;
        let val = this.topFilterList.find(x => x.fieldName === 'outMileage').initialValue;
        if (val < data) {
          this.$notify.warning({ title: '提示信息', message: '出厂行驶里程不能小于进厂里程' });
          return;
        }
        const s = {
          outMileage: this.topFilterList.find(item => item.fieldName === 'outMileage').initialValue,
          technician: this.topFilterList.find(item => item.fieldName === 'technician').initialValue,
          checker: this.topFilterList.find(item => item.fieldName === 'technicianName').initialValue,
          id: '1',
          roNo: this.Mileage[0].RO_NO
        };
        let res = await maintainRepairAssignComplete(s);
        if (res.resultCode == 200) {
          this.$notify.success({ title: '提示信息', message: '竣工成功!' });
          this.$emit('close', this.Mileage[0]._uid);
        }
      } else {
        this.$notify.warning({ title: '提示信息', message: '请输入出厂行驶里程!' });
      }
    },
    async serviceConsultant() {
      let testDrive = { role: dictCode.TEST_DRIVE };
      let teacer = { role: dictCode.TRACER };
      let testDriveRes = await searchRoleByCode(testDrive);
      let teacerRes = await searchRoleByCode(teacer);
      this.topFilterList.find(item => item.fieldName === 'technician').itemList = testDriveRes.data.map(x => ({ text: x.employeeName, value: x.userId }));
      this.topFilterList.find(item => item.fieldName === 'technicianName').itemList = teacerRes.data.map(x => ({ text: x.employeeName, value: x.userId }));
    },
    closePage() {
      this.$emit('close', this.Mileage[0]._uid);
    }
  }
};
</script>
